package commands;

import java.util.Date;

import containers.StudentContainer;
import dbadapters.ChangeStudentdb;
import entities.PaymentPlan;
import entities.Student;

public class CreatePaymentPlan {

	/** Payment plan for the student */
	PaymentPlan plan;

	/** Student to create the payment plan for */
	Student student;

	/** Id of the student to create payment plan for */
	int sId;

	/**
	 * Constructor
	 * 
	 * @param i
	 *            id of the student to create the payment plan for
	 * 
	 *            The plan creates loan payments each month of loanAmount/12 The
	 *            first date of payment is the first of the next month from when
	 *            the plan was created.
	 */
	public CreatePaymentPlan(int i) {
		sId = i;
		student = null;

		if (StudentContainer.dictionary().containsKey(sId)) {
			student = StudentContainer.dictionary().get(sId);
		}

		if (student != null) {
			plan = new PaymentPlan(student.getLoan().getAmountGiven());
			plan.setAmountReceived(0.0);
			plan.setPrevAmount(0.0);
			plan.setPrevDate(null);
			plan.setNextAmount(plan.getMonthlyPayment());

			// String DATE_FORMAT_NOW = "MM/DD/YYYY";

			Date curDate = new Date();
			// SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);

			// System.out.println("Now : " + sdf.format(curDate.getTime()));

			int year = curDate.getYear();
			int month = curDate.getMonth();
			int day = curDate.getDate();

			// month is December
			if (month != 11) {
				plan.setNextDate(new Date(year, month + 1, 1));
			} else {
				plan.setNextDate(new Date(year + 1, 1, 1));
			}

			// attach the payment plan to the student - container
			student.setPaymentPlanCreated(true);
			student.setPaymentPlan(plan);
			if (StudentContainer.dictionary().containsKey(sId) == true) {
				Student nStudent = StudentContainer.dictionary().get(sId);
				StudentContainer.dictionary().remove(sId);
				StudentContainer.dictionary().put(sId, student);
			}

			// attach the payment plan to the student - database
			new ChangeStudentdb(student);
		}

	}

	public PaymentPlan getPaymentPlan() {
		return plan;
	}

	/**
	 * regression tests
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		CreatePaymentPlan myPlan = new CreatePaymentPlan(123456);

		myPlan.getPaymentPlan().getNextDateAsString();
		myPlan.getPaymentPlan().setNextDateAsString(
				myPlan.getPaymentPlan().getNextDateAsString());

		if (StudentContainer.dictionary().containsKey(123456)) {
			Student myStudent = StudentContainer.dictionary().get(123456);
			// System.out.println("NAme: " + myStudent.getName());
			if (myStudent.getPaymentPlan().getLoanAmount() != myPlan
					.getPaymentPlan().getLoanAmount()) {
				System.out
						.println("ERROR: Creating Payment Plan - Loan Amount");
			}

			if (myStudent.getPaymentPlan().getMonthlyPayment() != myPlan
					.getPaymentPlan().getMonthlyPayment()) {
				System.out
						.println("ERROR: Creating Payment Plan - Monthly Payment");
			}

			if (myStudent.getPaymentPlan().getAmountReceived() != myPlan
					.getPaymentPlan().getAmountReceived()) {
				System.out
						.println("ERROR: Creating Payment Plan - Amount Received");
			}

			if (myStudent.getPaymentPlan().getPrevAmount() != myPlan
					.getPaymentPlan().getPrevAmount()) {
				System.out
						.println("ERROR: Creating Payment Plan - Previous Amount");
			}

			if (myStudent.getPaymentPlan().getPrevDate() != null) {
				System.out
						.println("ERROR: Creating Payment Plan - Previous Date");
			}

			if (myStudent.getPaymentPlan().getNextAmount() != myPlan
					.getPaymentPlan().getNextAmount()) {
				System.out
						.println("ERROR: Creating Payment Plan - Next Amount");
			}

			if (myStudent.getPaymentPlan().getNextDate() != myPlan
					.getPaymentPlan().getNextDate()) {
				System.out.println("ERROR: Creating Payment Plan - NExt Date");
			}
		}
	}
}
